﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcVehicleInsurance.Models;

namespace MvcVehicleInsurance.Areas.Admin.Controllers
{
    public class Cus_PoliController : Controller
    {
        private DataClassesVehicleDataContext data = new DataClassesVehicleDataContext();
        //
        // GET: /Admin/Cus_Poli/

        public ActionResult Index()
        {
            List<MvcVehicleInsurance.Models.CustomerPolicyRecord> listcpr = (from cpr in data.CustomerPolicyRecords select cpr).ToList();
            if (listcpr.Count > 0)
            {
                return View(listcpr);
            }

            return View();
        }

        //
        // GET: /Admin/Cus_Poli/Details/5

        public ActionResult Details(int id)
        {
            CustomerPolicyRecord details_cpr = data.CustomerPolicyRecords.Where(dt => dt.policyNumber == id.ToString()).First();
            if (ModelState.IsValid)
            {
                return View(details_cpr);
            }

            return View();
        }

        //
        // GET: /Admin/Cus_Poli/Create

        public ActionResult Create()
        {
            CustomerPolicyRecord ctp = new CustomerPolicyRecord();
            return View();
        }

        //
        // POST: /Admin/Cus_Poli/Create

        [HttpPost]
        public ActionResult Create(CustomerPolicyRecord collection, [Bind(Exclude = "policyNumber")] CustomerPolicyRecord cpr)
        {
            if (ModelState.IsValid)
            {
                data.CustomerPolicyRecords.InsertOnSubmit(collection);
                data.SubmitChanges();
            }

            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/Cus_Poli/Edit/5

        public ActionResult Edit(int id)
        {
            CustomerPolicyRecord edit_cpr = data.CustomerPolicyRecords.First(fx => fx.policyNumber == id.ToString());
            return View(edit_cpr);
        }

        //
        // POST: /Admin/Cus_Poli/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, CustomerPolicyRecord collection)
        {
            CustomerPolicyRecord edit_cpr = data.CustomerPolicyRecords.First(c => c.policyNumber == id.ToString());
            if (ModelState.IsValid)
            {
                edit_cpr.policyNumber = collection.policyNumber;
                edit_cpr.policydate = collection.policydate;
                edit_cpr.policyDuration = collection.policyDuration;
                edit_cpr.policyDescribe = collection.policyDescribe;
                edit_cpr.CompanyID = collection.CompanyID;
                data.SubmitChanges();
            }

            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        public ActionResult Delete(int id, CustomerPolicyRecord collection)
        {
            CustomerPolicyRecord edit_cpr = data.CustomerPolicyRecords.First(lt => lt.policyNumber == id.ToString());
            if (ModelState.IsValid)
            {
                data.CustomerPolicyRecords.DeleteOnSubmit(edit_cpr);
                data.SubmitChanges();
            }
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
